<?php
/**
 * 文本投票
 * @author 回忆QQ125879930
 * @url
 */
defined('IN_IA') or exit('Access Denied');
header("Content-Type: text/html; charset=UTF-8");

require("huiyi.support.php");

class Huiyi_textvoteModuleSite extends WeModuleSite {

    public $table_prefix='huiyi_textvote_';
    public $table_reply='huiyi_textvote_reply';
    public $table_setting='huiyi_textvote_setting';
    public $table_option='huiyi_textvote_option';
    public $table_log='huiyi_textvote_log';

    public $web_root = './source/modules/huiyi_textvote/template/';
    public $mobile_root = './source/modules/huiyi_textvote/template/mobile/';


    public function doWebVotelist() {
        global $_W,$_GPC;
        $weid = $_W['weid'];
        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        //顶部搜索条件
        $condition = '';
        if (!empty($_GPC['keyword'])) {
            $condition .= " AND title LIKE '%{$_GPC['keyword']}%'";
        }

        $list = pdo_fetchall("SELECT * FROM ".tablename($this->table_setting)." WHERE weid = '{$weid}' $condition ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);
        $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->table_setting) . " WHERE weid = '{$weid}' $condition");
        $pager = pagination($total, $pindex, $psize);

        include $this->template('votelist');
    }

    /**删除**/
    public function doWebVotedelete() {
        global $_GPC, $_W;
        $weid=$_W['weid'];
        $id = intval($_GPC['id']);
        $item = pdo_fetch("SELECT * FROM ".tablename($this->table_setting)." WHERE id = :id and weid=:weid" , array(':id' => $id,':weid'=>$_W['weid']));
        if (empty($item)) {
            message('抱歉，所选数据不存在或是已经删除！', '', 'error');
        }
        pdo_delete($this->table_setting, array('id' => $item['id']));
        message('删除成功！', referer(), 'success');
    }


    /**增加/编辑**/
    public function doWebVoteadd() {
        global $_GPC, $_W;
        $weid=$_W['weid'];
        $id = intval($_GPC['id']);

        if (!empty($id))
        {
            $item = pdo_fetch("SELECT * FROM ".tablename($this->table_setting)." WHERE id = :id", array(':id' => $id));
            if (empty($item))
            {
                message('抱歉，所选数据不存在或是已经删除！', '', 'error');
            }
        }

        if (checksubmit('submit')) {
            if (empty($_GPC['title'])) {
                message('请输入活动名称！');
            }
            $data = array(

                'weid' => $_W['weid'],
                'state' => intval($_GPC['state']),
                'create_time' => TIMESTAMP,
                'use_count' => intval($_GPC['use_count']),

                'title' => $_GPC['title'],
                'picture' => $_GPC['picture'],
                'description' => $_GPC['description'],

                'start_time' => empty($_GPC['start_time']) ? TIMESTAMP : strtotime($_GPC['start_time']),
                'end_time' => empty($_GPC['end_time']) ? TIMESTAMP + 86399*7 : strtotime($_GPC['end_time']),
                'max_vote_day' => intval($_GPC['max_vote_day']),
                'max_vote_count' => intval($_GPC['max_vote_count']),
                'once_vote' => intval($_GPC['once_vote']),
                'interval_time' => intval($_GPC['interval_time']),
            );

            if (empty($id)) {
                pdo_insert($this->table_setting, $data);
            } else {
                unset($data['create_time']);
                pdo_update($this->table_setting, $data, array('id' => $id));
            }
            message('信息更新成功！', $this->createWebUrl('votelist', array()), 'success');

        }

        include $this->template('voteadd');
    }

    /**增加/编辑**/
    public function doWebOptionadd() {
        global $_GPC, $_W;
        $weid=$_W['weid'];
        $id = intval($_GPC['id']);

        if (!empty($id))
        {
            $vote = pdo_fetch("SELECT * FROM ".tablename($this->table_setting)." WHERE id = :id", array(':id' => $id));
            if (empty($vote))
            {
                message('抱歉，所选数据不存在或是已经删除！', '', 'error');
            }
            $options = pdo_fetchall("SELECT * FROM ".tablename($this->table_option)." WHERE vid = :vid ORDER BY code", array(':vid' => $id));
        }

        if (checksubmit('submit')) {

            //选项
            //更新
            if (!empty($_GPC['option-title'])) {
                foreach ($_GPC['option-title'] as $index => $title) {
                    if (empty($title)) {
                        continue;
                    }

                    //处理标题和描述
                    $update = array(
                        'title' => $title,
                        'code' => intval($_GPC['option-code'][$index]),
                        'state' => intval($_GPC['option-state'][$index]),
                    );

                    pdo_update($this->table_option, $update, array('id' => $index));
                }
            }

            //添加
            if (!empty($_GPC['option-title-new'])) {
                foreach ($_GPC['option-title-new'] as $index => $title) {
                    if (empty($title)) {
                        continue;
                    }

                    //处理标题和描述
                    $insert = array(
                        'weid' => $_W['weid'],
                        'vid' => $id,
                        'title' => $title,
                        'code' => intval($_GPC['option-code-new'][$index]),
                        'state' => intval($_GPC['option-state-new'][$index]),
                    );

                    pdo_insert($this->table_option, $insert);
                }
            }

            message('信息更新成功！', $this->createWebUrl('optionadd', array('id' => $id) ), 'success');

        }



        include $this->template('optionadd');
    }

    public function doWebVoteResult() {
        global $_GPC, $_W;

        $weid=$_W['weid'];
        $id = intval($_GPC['id']);

        if (!empty($id))
        {
            $vote = pdo_fetch("SELECT * FROM ".tablename($this->table_setting)." WHERE id = :id", array(':id' => $id));
            if (empty($vote))
            {
                message('抱歉，所选数据不存在或是已经删除！', '', 'error');
            }
            $options = pdo_fetchall("SELECT * FROM ".tablename($this->table_option)." WHERE vid = :vid ORDER BY code", array(':vid' => $id));
        }

        include $this->template('voteresult');
    }

    public function doWebVoteLog() {
        global $_GPC, $_W;
        $weid=$_W['weid'];
        $id = intval($_GPC['id']);
        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        //顶部搜索条件
        $condition = '';
        if (!empty($_GPC['keyword'])) {
            $condition .= " AND option_title LIKE '%{$_GPC['keyword']}%'";
        }

        if (!empty($id))
        {
            $vote = pdo_fetch("SELECT * FROM ".tablename($this->table_setting)." WHERE id = :id", array(':id' => $id));
            if (empty($vote))
            {
                message('抱歉，所选数据不存在或是已经删除！', '', 'error');
            }
        }


        $list = pdo_fetchall("SELECT * FROM ".tablename($this->table_log)." WHERE weid = '{$weid}' AND vid = $id $condition ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);
        $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->table_log) . " WHERE weid = '{$weid}' AND vid = $id $condition");
        $pager = pagination($total, $pindex, $psize);

        include $this->template('votelog');
    }

    /***Ajax启用禁用状态***/
    public function doWebState() {
        global $_GPC;
        $id = intval($_GPC['id']);
        $tablename = $_GPC['tablename'];
        $data = array(
            'state' => $_GPC['state'],
        );

        if(pdo_update($this ->table_prefix.$tablename,$data,array('id' => $id))) {
            message('模块操作成功！', referer(), 'success');
        }
        else {
            message('请保存规则后再进行设置！', referer(), 'success');
        }
    }

}
